package org.github.manx98.jlibhac.fs;

import java.io.EOFException;
import java.io.IOException;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/3 0:54
 */
public class SubStorage extends IStorage {
    private final IStorage baseStorage;
    private final long offset;
    private final long size;

    public SubStorage(IStorage storage, long offset, long size) {
        super(0);
        if (offset < 0 || size < 0) {
            throw new IllegalArgumentException("offset and size must be positive");
        }
        this.baseStorage = storage;
        this.offset = offset;
        this.size = size;
    }

    @Override
    public void read(long offset, byte[] destination, int destOffset, int len) throws IOException {
        if (offset + len > size) {
            throw new EOFException();
        }
        this.baseStorage.read(this.offset + offset, destination, destOffset, len);
        this.position = offset + len;
    }

    @Override
    public long getSize() {
        return size;
    }
}
